package com.example.toolproject.demo.controller;

import com.example.toolproject.demo.domain.DataType;
import com.example.toolproject.demo.domain.User;
import com.example.toolproject.demo.service.DataTypeService;
import com.example.toolproject.demo.util.ResultBean;
import com.example.toolproject.demo.util.jwt.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName DataTypeController
 * @Description TODO
 * @Author lvyongwei
 * @Date 2020-06-19 16:00
 **/
@Controller
@CrossOrigin
@RequestMapping("/dataType")
public class DataTypeController {

    @Autowired
    private DataTypeService dataTypeService;

    @GetMapping("/getAllDataType")
    @ResponseBody
    public ResultBean getAllDataType(HttpServletRequest request){
        //获取前端传来的token
        String token = request.getHeader("Authorization");
        if(token==null || token.equals("")){
            return ResultBean.error("无token,请先登录");
        }
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        //判断token是否失效
        if(u == null){
            return ResultBean.error("token无效");
        }
        //执行查询全部方法
        List<DataType> list=dataTypeService.getAllDataType();
        if(list==null || list.size()==0){
            return  ResultBean.error("没有标题信息");
        }
        return ResultBean.success(list);
    }
}
